package handlers

import (
	"backend/pkg/log"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func Logout(c *gin.Context) {
	startTime := time.Now()

	// Get the user ID from the context (set by auth middleware)
	userID, exists := c.Get("user_id")
	if !exists {
		log.Warning("Logout attempt without user_id in context")
		c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
		log.RequestLog(c, startTime, "Unauthorized logout attempt")
		return
	}

	// Log the successful logout
	log.Info("User logged out successfully: id=%v", userID)
	log.RequestLog(c, startTime, "Logout successful")

	c.JSON(http.StatusOK, gin.H{"message": "Logged out successfully"})
}
